package com.rockcent.job.task.demo;

import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.Banner;
import com.rockcent.mall.lib.domain.Product;
import com.rockcent.mall.lib.domain.ProductQuantity;
import com.rockcent.mall.lib.repository.BannerRepository;
import com.rockcent.mall.lib.repository.MerchantProductRepository;
import com.rockcent.mall.lib.repository.ProductQuantityRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * cron例子
 * Created by zhangjh on 2016/11/23.
 */
//@Component
public class ScheduledCronSequenceRateTasks {

    private static final Logger log = LoggerFactory.getLogger(ScheduledFixedRateTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private BannerRepository bannerRepository;


    @Autowired
    private MerchantProductRepository merchantProductRepository;

    @Autowired
    private ProductQuantityRepository productQuantityRepository;

    /**
     * Example patterns:
     * <p>
     * "0 0 * * * *" = the top of every hour of every day.
     **/
    @Scheduled(cron = "0/5 * * * * *")
    public void reportCurrentTime() {
        log.info("======================>The time is now {}", dateFormat.format(new Date()));
        List<ProductQuantity> quantities = productQuantityRepository.findAll();
        for (ProductQuantity quantity : quantities) {
            Long productId = quantity.getProductId();

            List<Product> products = merchantProductRepository.findByMarketId(productId);
            if (null == products || products.size()==0) {
                continue;
            }
            for(Product product:products){
                product.setProductQuantityId(quantity.getId());
                merchantProductRepository.save(product);
            }
        }
//        Banner banner = bannerRepository.findTop1ByIsDeleteOrderBySeqNoDesc(Whether.NO);
//        log.info(banner.toString());
    }

    /**
     * Example patterns:
     * <p>
     * "0 0/5 * * * *" = the top of every hour of every day.
     **/
//    @Scheduled(cron = "0 0/5 * * * *")
    public void reportCurrentTime2() {
        log.info("======================>The time is now {}", dateFormat.format(new Date()));
//        Banner banner = bannerRepository.findTop1ByIsDeleteOrderBySeqNoDesc(Whether.NO);
//        log.info(banner.toString());
    }

}
